# app/routes/appointment_routes.py
from flask import Blueprint, request, jsonify
from app.services.appointment_service import AppointmentService

appointment_bp = Blueprint('appointments', __name__)

@appointment_bp.route('/providers', methods=['GET'])
def get_available_providers():
    date = request.args.get('date')
    time_slot = request.args.get('timeSlot')
    appointment_type = request.args.get('appointmentType')
    
    try:
        providers = AppointmentService.get_available_providers(date, time_slot, appointment_type)
        
        return jsonify({
            'providers': [
                {
                    'id': provider.id,
                    'name': provider.name,
                    'type': provider.type
                } for provider in providers
            ]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 400

@appointment_bp.route('/appointments', methods=['POST'])
def create_appointment():
    data = request.get_json()
    
    try:
        # For mock data, we'll use a dummy user_id
        user_id = 1  # Hardcoded user ID for demonstration
        
        appointment = AppointmentService.create_appointment(
            user_id=user_id,
            provider_id=data['providerId'],
            date_str=data['date'],
            time_slot=data['timeSlot'],
            appointment_type=data['appointmentType']
        )
        
        return jsonify({
            'message': 'Appointment created successfully',
            'appointmentId': appointment.id
        }), 201
    except Exception as e:
        return jsonify({'error': str(e)}), 400

@appointment_bp.route('/appointments', methods=['GET'])
def get_user_appointments():
    # For mock data, we'll use a dummy user_id
    user_id = 1  # Hardcoded user ID for demonstration
    
    appointments = AppointmentService.get_user_appointments(user_id)
    
    return jsonify({
        'appointments': [
            {
                'id': appt.id,
                'date': appt.date,
                'timeSlot': appt.time_slot,
                'appointmentType': appt.appointment_type,
                'status': appt.status
            } for appt in appointments
        ]
    })